package my.cn.bdqn.life;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import my.cn.bdqn.life.data.DataManager;
import my.cn.bdqn.life.service.FreeService;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class WatchMovieActivity extends Activity {
    private JSONObject json;
    public static List<DataManager.Movie> data=new ArrayList();
	private ListView movieList;
	private BaseAdapter adapter;
	private Runnable getPhoto=new Runnable(){
		public void run(){
			getData();
		}
	};
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case 0:
				
				break;
			}
		}
	};
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.movie_layout);
		movieList=(ListView)findViewById(R.id.movielist);
		getAdapter();
		movieList.setAdapter(adapter);
		new Thread(getPhoto).start();
		movieList.setOnItemClickListener(new ListView.OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(WatchMovieActivity.this,DetailMovie.class);
				intent.putExtra("data", pos);
				startActivity(intent);
			}});
	}
    private void getAdapter(){
    	adapter=new BaseAdapter(){

			public int getCount() {
				// TODO Auto-generated method stub
				return data.size();
			}

			public Object getItem(int pos) {
				// TODO Auto-generated method stub
				return data.get(pos);
			}

			public long getItemId(int pos) {
				// TODO Auto-generated method stub
				return pos;
			}

			public View getView(int pos, View view, ViewGroup arg2) {
				// TODO Auto-generated method stub
				view=getLayoutInflater().inflate(R.layout.movie_item_layout,null);
				ImageView photo,tag;
				TextView name,type,player,time;
				photo=(ImageView)view.findViewById(R.id.photo);
				tag=(ImageView)view.findViewById(R.id.tag);
				name=(TextView)view.findViewById(R.id.name);
				type=(TextView)view.findViewById(R.id.label);
				player=(TextView)view.findViewById(R.id.player);
				time=(TextView)view.findViewById(R.id.time);
				DataManager.Movie m=data.get(pos);
				name.setText(m.getName());
				type.setText(m.getType());
				player.setText(m.getPlayer());
				time.setText(m.getTime());
				Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/freelife/"+m.getimage());
				photo.setImageBitmap(bm);
				if(pos<4){
					tag.setImageResource(R.drawable.hot);
				}
				return view;
			}    		
    	};    	
    }
	private void getData(){
		json=FreeService.back;
		try{
			JSONArray array=json.getJSONArray("list");
			for(int i=0;i<array.length();i++){
				JSONObject js=array.getJSONObject(i);
				DataManager.Movie m=
					new DataManager.Movie(js.getInt("mid"),
							js.getString("name"),
							js.getString("type"),
							js.getString("time"),
							js.getString("player"),
							js.getInt("image")
							);
				int image=m.getimage();
				File file=new File("/mnt/sdcard/freelife/"+image);
				if(!file.exists()){
					URL url=new URL("http://118.186.211.92:8080/LeisureLifeServer/dealcmd?cmd=4&image="+image);
					InputStream is=url.openStream();
					if(!file.getParentFile().exists()){
						file.getParentFile().mkdirs();
					}
					FileOutputStream out=new FileOutputStream(file);
					byte[]buf=new byte[1024];
					int len;
					while((len=is.read(buf))!=-1){
						out.write(buf,0,len);
					}
					out.close();
					is.close();					
				}
				synchronized (data) {
					data.add(m);
					adapter.notifyDataSetChanged();
				}				
			}			
		}catch(Exception e){}
	}
}







